﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;

namespace SaleSystem.Business
{
    public partial class frm_MyBackBillList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bnNew.OnClientClick = win_Content.GetShowReference("frm_MyBackBillEdit.aspx?Id=0", "新增返货单");
                BindDropDownList();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();
            }
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }

        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                Sale_Model.MyBackBill SM = new Sale_Model.MyBackBill();

                SM.MBB_Id = ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                SM.MBB_Code = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                Manager.GetModel(SM);
                if (SM.MBB_Output == 1)
                {

                    ExtAspNet.Alert.Show("该返货单已经出库，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
                }
                else
                {
                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        GetAndBind();
                        ExtAspNet.Alert.Show("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                    }
                    else
                        ExtAspNet.Alert.Show("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                }

            }
            else if (e.CommandName.ToLower() == "edit")
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_MyBackBillEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);

            }
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        protected void gv_List_PreRowDataBound(object sender, ExtAspNet.GridPreRowEventArgs e)
        {
            ExtAspNet.LinkButtonField lbfAction1 = gv_List.FindColumn("edit") as ExtAspNet.LinkButtonField;
            ExtAspNet.LinkButtonField lbfAction2 = gv_List.FindColumn("delete") as ExtAspNet.LinkButtonField;
            DataRowView row = e.DataItem as DataRowView;
            if (ValueHandler.GetIntNumberValue(row["MBB_Output"]) == 1)
            {
                //                lbfAction1.Enabled = false;
                if (ValueHandler.GetIntNumberValue(row["MBB_State"]) == 1)
                {
                    lbfAction1.Text = "查看";
                }
                else
                {
                    lbfAction1.Text = "结算";
                }
                lbfAction2.Enabled = false;

            }
            else
            {
                lbfAction1.Text = "编辑";

                lbfAction1.Enabled = true;
                lbfAction2.Enabled = true;
            }


        }
        #endregion

        #region Method

        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition, gv_List.PageIndex + 1, ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            strCondition = "";
            if (tb_MBB_Code.Text.Trim() != "")
            {
                strCondition += " MBB_Code LIKE '%" + ValueHandler.GetStringValue(tb_MBB_Code.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (ddl_MBB_Supp_Id.SelectedValue != "-1")
            {
                strCondition += " MBB_Supp_Id=" + ddl_MBB_Supp_Id.SelectedValue + " AND ";
            }
            if (dp_MBB_Date1.Text.Trim() != "")
            {
                DateTime d1 = DateTime.Now;
                if (DateTime.TryParse(dp_MBB_Date1.Text, out d1))
                {
                    strCondition += " MBB_Date >='" + dp_MBB_Date1.Text + "' AND ";
                }
            }
            if (dp_MBB_Date2.Text.Trim() != "")
            {
                DateTime d2 = DateTime.Now;
                if (DateTime.TryParse(dp_MBB_Date2.Text, out d2))
                {
                    strCondition += " MBB_Date <='" + dp_MBB_Date2.Text + "' AND ";
                }
            }
            if (nb_MBB_Money1.Text.Trim() != "")
            {
                strCondition += " MBB_Money >=" + nb_MBB_Money1.Text + " AND ";
            }
            if (nb_MBB_Money2.Text.Trim() != "")
            {
                strCondition += " MBB_Money <=" + nb_MBB_Money2.Text + " AND ";
            }
            if (ddl_MBB_SUser_Id.SelectedValue != "-1")
            {
                strCondition += " MBB_SUser_Id =" + ddl_MBB_SUser_Id.SelectedValue + " AND ";
            }
            if (ddl_MBB_Output.SelectedValue != "-1")
            {
                strCondition += " MBB_Output =" + ddl_MBB_Output.SelectedValue + " AND ";
            }
            if (ddl_MBB_State.SelectedValue != "-1")
            {
                strCondition += " MBB_State=" + this.ddl_MBB_State.SelectedValue + " AND ";
            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        private void BindDropDownList()
        {
            SysUsersOp userManager = new SysUsersOp();
            DataTable dtUser = userManager.GetList("");
            this.ddl_MBB_SUser_Id.DataSource = dtUser;
            this.ddl_MBB_SUser_Id.DataBind();
            this.ddl_MBB_SUser_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
            SupplierOp supplierManager = new SupplierOp();
            DataTable dtSupplier = supplierManager.GetList("");
            this.ddl_MBB_Supp_Id.DataSource = dtSupplier;
            this.ddl_MBB_Supp_Id.DataBind();
            this.ddl_MBB_Supp_Id.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
        }
        #endregion

        #region Property
        MyBackBillOp Manager = new MyBackBillOp();

        #endregion
    }
}
